package com.blue.utils;

import de.innosystec.unrar.Archive;
import de.innosystec.unrar.rarfile.FileHeader;

import java.io.File;
import java.io.FileOutputStream;

public class RarCompressUtils {

	public static void main(String[] args) {  
		
		RarCompressUtils testUnRar = new RarCompressUtils();
		
		try {
			testUnRar.unrar("E:\\home\\BLUE\\apache-tomcat-7.0.5.rar","F:\\cygwin\\");
			//testUnRar.unrar("E:\\home\\BLUE\\bak.rar", "E:\\home\\BLUE\\testrar\\");
			
			//RARFile("E:\\home\\BLUE\\bak\\*","test_jh","E:\\home");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	/* 
	    * cmd 压缩与解压缩命令 
	*/  
	private static String rarCmd = "C:\\Program Files (x86)\\WinRAR\\Rar.exe a ";
	/** 
	    * 将1个文件压缩成RAR格式 
	    * rarName 压缩后的压缩文件名(不包含后缀) 
	    * fileName 需要压缩的文件名(必须包含路径) 
	    * destDir 压缩后的压缩文件存放路径 
	    */  
	
	public static void RARFile(String rarName, String fileName, String destDir) {  
	       rarCmd += destDir +"\\"+ fileName + ".rar " +" "+ rarName;  
	       System.out.println(rarCmd);
	       try {  
	           Runtime rt = Runtime.getRuntime();  
	           Process p = rt.exec(rarCmd);  
	       }catch(Exception e) {  
	           System.out.println(e.getMessage());        
	       }  
	}    
	    
	/**   
	    * 解压rar格式压缩包。   
	    * 对应的是java-unrar-0.3.jar，但是java-unrar-0.3.jar又会用到commons-logging-1.1.1.jar   
	    */    
	   public void unrar(String sourceRar,String destDir) throws Exception{     
	       Archive a = null;     
	       FileOutputStream fos = null;     
	       try{     
	           a = new Archive(new File(sourceRar));     
	           FileHeader fh = a.nextFileHeader();     
	           while(fh!=null){     
	               if(!fh.isDirectory()){     
	                   //1 根据不同的操作系统拿到相应的 destDirName 和 destFileName     
	                   String compressFileName = fh.getFileNameString().trim();     
	                   String destFileName = "";     
	                   String destDirName = "";     
	                   //非windows系统     
	                   if(File.separator.equals("/")){     
	                       destFileName = destDir + compressFileName.replaceAll("\\\\", "/");     
	                       destDirName = destFileName.substring(0, destFileName.lastIndexOf("/"));     
	                   //windows系统      
	                   }else{     
	                       destFileName = destDir + compressFileName.replaceAll("/", "\\\\");     
	                       destDirName = destFileName.substring(0, destFileName.lastIndexOf("\\"));     
	                   }     
	                   //2创建文件夹     
	                   File dir = new File(destDirName);     
	                   if(!dir.exists()||!dir.isDirectory()){     
	                       dir.mkdirs();     
	                   }     
	                   //3解压缩文件     
	                   fos = new FileOutputStream(new File(destFileName));     
	                   a.extractFile(fh, fos);     
	                   fos.close();     
	                   fos = null;     
	               }     
	               fh = a.nextFileHeader();     
	           }     
	           a.close();     
	           a = null;     
	       }catch(Exception e){     
	           throw e;     
	       }finally{     
	           if(fos!=null){     
	               try{fos.close();fos=null;}catch(Exception e){e.printStackTrace();}     
	           }     
	           if(a!=null){     
	               try{a.close();a=null;}catch(Exception e){e.printStackTrace();}     
	           }     
	       }     
	   }     
}
